//
//  HMVisitorView.swift
//  Weibo
//
//  Created by xiongminghua on 16/7/18.
//  Copyright © 2016年 熊明华. All rights reserved.
//

import UIKit

// 使用代理
// 1.定义代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    
    //2.定义代理方法
    /// 点击了注册
    func visitorViewDidRegister()
    
    /// 点击了登录
    func visitorViewDidLogin()
}


/// 自定义访客视图
class HMVisitorView: UIView {
    
    // 3.设置代理属性
    weak var delegate: HMVisitorViewDelegate?

    // 通过storyboard或xib创建对象，调用的方法 ,重写了下面的方法，就必须要实现这个方法，否则报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 通过代码创建对象，会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置背景颜色 , 当RGB的颜色一致时，就可以直接使用white，来加颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 设置UI界面
        self.setupUI()
        
        
    }
    
    // MARK: - 外部方法
    /// 设置不同界面显示不同的图片
    func setupVisitorInfo(imageName: String, message: String) {
        
        // 隐藏房子
        houseView.hidden = true
        
        // 隐藏遮盖
       // coverView.hidden = true
        // 或者将遮罩放在最后
        self.sendSubviewToBack(coverView)
        
        // 赋值
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
    }
    
    /// 让首页开启动画
    func setupAnimation() {
        
        startAnimation()
    }
    
    // MARK: - 私有方法
    
    /**
        给转轮添加核心动画
    */
    private func startAnimation() {
        
        // 创建核心动画 , CATransform3D Key Paths 可以在文档中查找动画的属性
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // 设置动画的属性
        animation.toValue = 2 * M_PI
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        // 当动画完成之后不移除这个动画
        animation.removedOnCompletion = false
        
        //将动画添加到图层
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    /**
    *  设置UI界面
    */
   private func setupUI() {
        
        // 设置约束的前提条件是一定要有父控件
        
        // 添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        // 设置约束，手动添加
        // 使用纯代码约束需要关闭Autoresizing的影响
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        /// 转轮
        /// CenterX和父控件的CenterX重合
        // NSLayoutConstraint一个约束对象
        // 将约束添加到父控件上
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // CenterY偏移40
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /// 遮罩
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 房子
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 消息label
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        /// 注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// 登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
    }
    
    // MARK: - 懒加载子控件
    
    /// 转轮图片
   private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:
        "visitordiscover_feed_image_smallicon"))
    
    /// 添加遮罩 , 上面是透明的，下面是不透明的图片
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 房子
   private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 信息label
   private lazy var messageLabel: UILabel = {
       
        let label = UILabel(frame: CGRectZero)
        
        // 设置属性
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "多关注一些人，你可以得到提高"
        // 自适应大小
        label.sizeToFit()
        
        return label
        
    }()
    
    /// 注册按钮
   private lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        // 设置
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
        // 添加按钮点击事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    
        return button
        
    }()
    
    /// 登录按钮
   private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        // 设置
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkTextColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
       // 添加点击事件
        button.addTarget(self, action: "loginButttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    
    // MARK: - 按钮点击事件
    // 注册按钮，因为需要进行控制器跳转，所有这里使用代理来实现
    // 添加@objc 修饰私有方法，可以让外界进行访问OC方法
   @objc private func registerButtonClick() {
        
        // 4.调用代理方法
        self.delegate?.visitorViewDidRegister()
        
    }
    /// 登录按钮
   @objc private func loginButttonClick() {
        
       self.delegate?.visitorViewDidLogin()
        
    }

}
